/*
 * Created on 08/02/2013 - 3:38 PM
 */
package sembr.vtk.data.input;

import sembr.vtk.persistence.ISavable;
import sembr.vtk.writer.VtkXmlDataSetWriter;
import vtk.vtkLinearSubdivisionFilter;
import vtk.vtkPolyData;
import vtk.vtkTriangleFilter;

/**
 *
 * @author adriano
 */
abstract public class SourceBase implements ISavable
{ 
    public SourceBase()
    {
        LoadVTKLibraries();
    }
    
    @Override
    public void saveXmlFormat(String fileName)
    {
        VtkXmlDataSetWriter testWriter = new VtkXmlDataSetWriter(fileName, getPolyData());
        testWriter.writeFile();
    }
       
    public vtkPolyData refineWaveGuideMesh(vtkPolyData pdTobeRefined, int nrOfSubdivisions)
    {        
        vtkPolyData triangulatedPd = triangulatePolyData(pdTobeRefined);
        triangulatedPd.Update();
        
        vtkLinearSubdivisionFilter subdivisor = new vtkLinearSubdivisionFilter();
        subdivisor.SetNumberOfSubdivisions(nrOfSubdivisions);
        subdivisor.SetInput(triangulatedPd);
        subdivisor.Update();
        
       return subdivisor.GetOutput();
    }
    
    public vtkPolyData triangulatePolyData(vtkPolyData polydata)
    {
        vtkTriangleFilter triangulator = new vtkTriangleFilter();
        triangulator.SetInput(polydata);
        triangulator.Update();
        
        return triangulator.GetOutput();             
    }   
    
    // TODO: need to be implemented
    abstract public vtkPolyData getPolyData();  
    
    private static void LoadVTKLibraries() 
    {       
        System.loadLibrary("vtkCommon");
        System.loadLibrary("vtkFiltering");
        System.loadLibrary("vtkGenericFiltering");
        System.loadLibrary("vtkGraphics");
        System.loadLibrary("vtkHybrid");
        System.loadLibrary("vtkImaging");
        System.loadLibrary("vtksys");
        System.loadLibrary("vtkexpat");
        System.loadLibrary("vtkfreetype");
        System.loadLibrary("vtkftgl");
        System.loadLibrary("vtkjpeg");
        System.loadLibrary("vtkzlib");
        System.loadLibrary("vtktiff");
        System.loadLibrary("vtkpng");
        System.loadLibrary("vtkDICOMParser");
        System.loadLibrary("vtkIO");
        System.loadLibrary("vtkRendering");
        System.loadLibrary("vtkVolumeRendering");
        System.loadLibrary("vtkCommonJava");
        System.loadLibrary("vtkFilteringJava");
        System.loadLibrary("vtkGenericFilteringJava");
        System.loadLibrary("vtkGraphicsJava");
        System.loadLibrary("vtkHybridJava");
        System.loadLibrary("vtkImagingJava");
        System.loadLibrary("vtkIOJava");
        System.loadLibrary("vtkGraphicsJava");
        System.loadLibrary("vtkRenderingJava");
        System.loadLibrary("vtkVolumeRenderingJava");
    }
}
